﻿using FPSO.HMI.Controls;
using FPSO.HMI.Utils;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Services;
using FPSO.Services.Impl;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace FPSO.HMI.Views.Dialog
{
    /// <summary>
    /// LoginDialog.xaml 的交互逻辑
    /// </summary>
    public partial class UserEdidDialog : Window
    {
		private DBUserInfoService g_Service = new DBUserInfoService();
		private int g_status = 1;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="status">0:create;1:update</param>
		public UserEdidDialog(int status)
        {
            InitializeComponent();
			g_status = status;
			if (g_status == 1)
			{
				string current_username = AuthUtil.GetLoginUser();
				UsernameTextBox.Text = current_username;
				UsernameTextBox.IsEnabled = false;
				PasswordTextBox.Password = string.Empty;
				PasswordConfirmTextBox.Password = string.Empty;
			}
			else
			{
				UsernameTextBox.IsEnabled = true;
				UsernameTextBox.Text = string.Empty;
				PasswordTextBox.Password = string.Empty;
				PasswordConfirmTextBox.Password = string.Empty;
			}
		}

        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取用户输入的用户名和密码
            string username = UsernameTextBox.Text;
            string password = PasswordTextBox.Password;
			string confirm_password = PasswordConfirmTextBox.Password;
			// 基本格式校验
			if (string.IsNullOrWhiteSpace(username))
            {
                MessageBox.Show("用户名不能为空！", "校验失败");
                return;
            }
            if (string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(confirm_password))
            {
                MessageBox.Show("密码不能为空！", "校验失败");
                return;
            }
            if (password.Length < 8 || confirm_password.Length < 8)
            {
                MessageBox.Show("密码长度不小于8位！", "校验失败");
                return;
            }
			if (password != confirm_password)
			{
				MessageBox.Show("确认密码和密码不同！", "校验失败");
				return;
			}
			bool inAdmin = (bool)chbIsAdmin.IsChecked;
			string encrypt_password = AuthUtil.EncryptStringToBytes_Aes(password);
			string decrypt_password = AuthUtil.DecryptStringFromBytes_Aes(encrypt_password);
			UserInfo new_user = new UserInfo
			{
				UserName = username,
				Password = encrypt_password,
				UserType = Convert.ToInt32(inAdmin),
			};
			if(g_status == 1)
			{
				//先找到这个用户，将userid赋上，才能正确更新
				UserInfo exist_user = g_Service.GetUserInfo(username);
				new_user.UserId = exist_user.UserId;
				new_user.UserCreateTime = exist_user.UserCreateTime;
				g_Service.UpdateUserInfo(new_user);
			}
			else
			{
				g_Service.AddUserInfo(new_user);
			}
			this.DialogResult = true;
		}

		/// <summary>
		/// 退出事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            //返回false以阻止后续流程并退出 
            this.DialogResult = false;
        }

        /// <summary>
        /// 窗口拖动事件
        /// </summary>
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                DragMove();
            }
        }
    }
}
